package com.sanpu.kxe.business.trade.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.trade.contract.GatheringContract;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.business.trade.interfaces.ICompleteInputListener;
import com.sanpu.kxe.business.trade.presenter.GatheringPresenter;
import com.sanpu.kxe.common.view.KeyboardView;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;
import com.sanpu.kxe.utils.SystemBarHelper;
import com.sanpu.kxe.utils.ToastUtil;

import java.math.BigDecimal;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：收款
 * Created by 薛勇军 on 2019/2/27.
 */
@Route(path = "/member/Gathering")
public class GatheringActivity extends BaseTitleActivity implements GatheringContract.View, ICompleteInputListener {

    @Bind(R.id.titleTv)
    TextView titleTv;
    @Bind(R.id.gatheringMoney)
    TextView gatheringMoney;
    @Bind(R.id.viewKeyboard)
    KeyboardView viewKeyboard;
    @Bind(R.id.tips)
    TextView vTipsText;
    @Bind(R.id.gatherIv)
    ImageView gatherIv;
    @Bind(R.id.headerView)
    RelativeLayout headerView;

    private GatheringContract.Presenter mPresenter;

    private String currentType = TradeConfig.GATHERING_QUICKPAY;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_gathering;
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) headerView.getLayoutParams();
        params.setMargins(0, barHeight, 0, 0);
        headerView.setLayoutParams(params);

        currentType = getIntent().getStringExtra(TradeConfig.INTENT_GATHERTYPE);
        if (TextUtils.equals(TradeConfig.GATHERING_PAY, currentType)) {
            titleTv.setText("商家收款");
            gatherIv.setBackground(ContextCompat.getDrawable(mContext, R.drawable.ic_gather_commercial));
        } else {
            titleTv.setText("个人收款");
            gatherIv.setBackground(ContextCompat.getDrawable(mContext, R.drawable.ic_gather_personal));
        }

        mPresenter = new GatheringPresenter(this);
        mPresenter.initData();

        viewKeyboard.setListener(this);
    }

    @OnClick({R.id.leftIv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.leftIv:
                finish();
                break;
        }
    }

    @Override
    public void onCompleteInput(String inputText) {
        if (!TextUtils.isEmpty(inputText)) {
            if (new BigDecimal(inputText).compareTo(new BigDecimal(1000000)) == 1) {
                ToastUtil.showTextViewPrompt("当前最大输入金额为1000000元");
                gatheringMoney.setText("1000000");
                viewKeyboard.setCurrentText("1000000");
                return;
            }
            gatheringMoney.setText(inputText);
        } else {
            gatheringMoney.setText("0.00");
        }
    }

    @Override
    public void onCompletePay() {
        mPresenter.onCompletePay(currentType, gatheringMoney.getText().toString());
    }

    @Override
    public void showMarqueeView(String tips) {
        if (!TextUtils.isEmpty(tips)) {
            vTipsText.setText(tips);
        }
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .navigationBarWithKitkatEnable(false)
                .init();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    public void setPresenter(GatheringContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
